package com.kfpanda.spring.batch.repository.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.util.Assert;


public class MapJobInstanceDaoSupport implements MapJobInstanceDao{
	private Collection<JobInstance> jobInstances = new CopyOnWriteArraySet<JobInstance>();

	private long currentId = 0;

	public void clear() {
		jobInstances.clear();
	}

	public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {

		Assert.state(getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist");

		JobInstance jobInstance = new JobInstance(currentId++, jobParameters, jobName);
		jobInstance.incrementVersion();
		jobInstances.add(jobInstance);

		return jobInstance;
	}

	public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {

		for (JobInstance instance : jobInstances) {
			if (instance.getJobName().equals(jobName) && instance.getJobParameters().equals(jobParameters)) {
				return instance;
			}
		}
		return null;

	}

	public JobInstance getJobInstance(Long instanceId) {
		for (JobInstance instance : jobInstances) {
			if (instance.getId().equals(instanceId)) {
				return instance;
			}
		}
		return null;
	}

	public List<String> getJobNames() {
		List<String> result = new ArrayList<String>();
		for (JobInstance instance : jobInstances) {
			result.add(instance.getJobName());
		}
		Collections.sort(result);
		return result;
	}

	public List<JobInstance> getJobInstances(String jobName, int start, int count) {
		List<JobInstance> result = new ArrayList<JobInstance>();
		for (JobInstance instance : jobInstances) {
			if (instance.getJobName().equals(jobName)) {
				result.add(instance);
			}
		}
		Collections.sort(result, new Comparator<JobInstance>() {
			// sort by ID descending
			public int compare(JobInstance o1, JobInstance o2) {
				return Long.signum(o2.getId() - o1.getId());
			}
		});

		int startIndex = Math.min(start, result.size());
		int endIndex = Math.min(start + count, result.size());
		return result.subList(startIndex, endIndex);
	}

	public JobInstance getJobInstance(JobExecution jobExecution) {
		return jobExecution.getJobInstance();
	}
	
	public synchronized void remove(JobInstance jobInstance){
		this.jobInstances.remove(jobInstance);
	}
	
	@Override
	public int getInstanceSize(String jobName) {
		int count = 0;
		for (JobInstance instance : jobInstances) {
			if (instance.getJobName().equals(jobName)) {
				count ++;
			}
		}
		return count;
	}
	
	public static void main(String[] args) {
		Collection<Integer> cc = new CopyOnWriteArraySet<Integer>();
		cc.add(111);
		cc.add(222);
		cc.add(333);
		List<Integer> list = new ArrayList<Integer>();
		for(Iterator<Integer> itr = cc.iterator(); itr.hasNext(); ){
			Integer it = itr.next();
			System.out.println(it);
			list.add(it);
		}
		list.remove(0);
		System.out.println(list.size()+"=="+cc.size());
		
	}

}
